using COW;
using GCommon;
using System.Collections;
using System.Collections.Generic;
using TALibraryInCSharp;
using UnityEngine;
using UnityEngine.UI;
using static TALibraryInCSharp.Core;

public class UIManager1 : MonoBehaviour
{
    public static UIManager1 Instance;
    //public UIMenu mUIMenu;
    [SerializeField] GameObject mYPrefab;
    [SerializeField] GameObject mDatePrefab;
    [SerializeField] int xcount = 20;
    [SerializeField] int ycount = 10;

    List<CSVBaseData> mList;
    public UIWin mWin;

    List<GameObject> mYLines =new List<GameObject>();
    List<GameObject> mDates = new List<GameObject>();

    private void Awake()
    {
        Instance = this;
    }

    // Start is called before the first frame update
    void Start()
    {
    }

    public void OnClickReset()
    {
        KLineManager1.Instance.ResetPos();
    }
    public void OnClickLeft()
    {
        KLineManager1.Instance.mPage-=1;
        if (KLineManager1.Instance.mPage < 0)
        {
            KLineManager1.Instance.mPage = 0;
        }
        KLineManager1.Instance.ReDrawData();
    }
    public void OnClickRight()
    {
        KLineManager1.Instance.mPage += 1;
        if (KLineManager1.Instance.mPage >= mList.Count)
        {
            KLineManager1.Instance.mPage = mList.Count-1;
        }
        KLineManager1.Instance.ReDrawData();
    }

    public void SetData(List<CSVBaseData> list, float lowest, float highest, float xfactor, float yfactor, int len)
    {
        for (int i = 0; i < mYLines.Count; i++)
        {
            GameObject.Destroy(mYLines[i].gameObject);
        }
        mYLines.Clear();
        for (int i = 0; i < mDates.Count; i++)
        {
            GameObject.Destroy(mDates[i].gameObject);
        }
        mDates.Clear();


        mList = list;
        mYPrefab.SetActive(false);
        var gapy = (highest - lowest) / (ycount);
        for (int i = 0; i < ycount; i++)
        {
            var go1 = GameObject.Instantiate(mYPrefab, mYPrefab.transform.parent);
            go1.SetActive(true);
            var vy = lowest + gapy * (i + 1);
            (go1.transform as RectTransform).anchoredPosition = new Vector2(0, (vy-lowest) * yfactor);
            go1.GetComponentInChildren<Text>().text = Mathf.RoundToInt(vy).ToString();
            go1.GetComponentInChildren<Text>().color = Color.black;
            mYLines.Add(go1);
        }

        mDatePrefab.SetActive(false);
        int gapx = len / xcount;
        int index = 0;
        for (int i = 1; i < len; i++)
        {
            if (i % gapx != 0) { continue; } 

            var line = list[i] as KLineData;
            var go = GameObject.Instantiate(mDatePrefab, mDatePrefab.transform.parent);
            go.SetActive(true);
            go.name = $"Date_{index}_{i}";
            (go.transform as RectTransform).anchoredPosition = new Vector2(xfactor * (i + 0.5f), 0);
            go.GetComponentInChildren<Text>().text = line.date;
            go.GetComponentInChildren<Text>().color = Color.black;
            index += 1;
            mDates.Add(go);
        }

    }
}
